﻿using System;
using System.Collections.Generic;
using System.Text;
using WindowsInput;

namespace Tornado
{
    public class MouseAction
    {
        static protected MouseSimulator mouse = new MouseSimulator();

        public virtual MouseExCode ExCode
        {
            get { return 0; }
        }

        public virtual void Process(byte[] arg) { }

    }

    public class SignalProcesserMouse : SignalProcesser
    {
        public override SignalCodes Code
        {
            get { return SignalCodes.MOUSE; }
        }

        public override void Process(ref SignalPacket packet)
        {
            foreach (var p in actions)
                if ((byte)p.ExCode == packet.ExtensionCode)
                {
                    p.Process(packet.Argument);
                    break;
                }
        }

        MouseAction[] actions = new MouseAction[]
        {
            new MouseMove(),
            new MouseLeftButtonDown(),
            new MouseLeftButtonUp(),
            new MouseLeftButtonClick(),
            new MouseLeftButtonDoubleClick(),
            new MouseRightButtonDown(),
            new MouseRightButtonUp(),
            new MouseRightButtonClick(),
            new MouseRightButtonDoubleClick(),
            new MouseVScroll(),
            new MouseHScroll()
        };
    }

    public class MouseVScroll : MouseAction
    {
        public override MouseExCode ExCode
        {
            get
            {
                return MouseExCode.VerticalScroll;
            }
        }

        public override void Process(byte[] arg)
        {
            if (arg[0] != 0)
                mouse.VerticalScroll((sbyte)arg[0]);
        }
    }

    public class MouseHScroll : MouseAction
    {
        public override MouseExCode ExCode
        {
            get
            {
                return MouseExCode.HorizontalScroll;
            }
        }

        public override void Process(byte[] arg)
        {
            mouse.HorizontalScroll(arg[0]);
        }
    }

    public class MouseMove : MouseAction
    {
        public override MouseExCode ExCode
        {
            get { return MouseExCode.Move; }
        }

        public override void Process(byte[] arg)
        {
            short dx = BitConverter.ToInt16(arg, 0);
            short dy = BitConverter.ToInt16(arg, 2);
            if (dx != 0 || dy != 0)
                mouse.MoveMouseBy(dx, dy);
        }
    }

    public class MouseLeftButtonDown : MouseAction
    {
        public override MouseExCode ExCode
        {
            get { return MouseExCode.LeftButtonDown; }
        }

        public override void Process(byte[] arg)
        {
            mouse.LeftButtonDown();
        }
    }

    public class MouseLeftButtonUp : MouseAction
    {
        public override MouseExCode ExCode
        {
            get { return MouseExCode.LeftButtonUp; }
        }

        public override void Process(byte[] arg)
        {
            mouse.LeftButtonUp();
        }
    }

    public class MouseLeftButtonClick : MouseAction
    {
        public override MouseExCode ExCode
        {
            get { return MouseExCode.LeftButtonClick; }
        }

        public override void Process(byte[] arg)
        {
            mouse.LeftButtonClick();
        }
    }

    public class MouseLeftButtonDoubleClick : MouseAction
    {
        public override MouseExCode ExCode
        {
            get { return MouseExCode.LeftButtonDoubleClick; }
        }

        public override void Process(byte[] arg)
        {
            mouse.LeftButtonDoubleClick();
        }
    }

    public class MouseRightButtonDown : MouseAction
    {
        public override MouseExCode ExCode
        {
            get { return MouseExCode.RightButtonDown; }
        }

        public override void Process(byte[] arg)
        {
            mouse.RightButtonDown();
        }
    }

    public class MouseRightButtonUp : MouseAction
    {
        public override MouseExCode ExCode
        {
            get { return MouseExCode.RightButtonUp; }
        }

        public override void Process(byte[] arg)
        {
            mouse.RightButtonUp();
        }
    }

    public class MouseRightButtonClick : MouseAction
    {
        public override MouseExCode ExCode
        {
            get { return MouseExCode.RightButtonClick; }
        }

        public override void Process(byte[] arg)
        {
            mouse.RightButtonClick();
        }
    }

    public class MouseRightButtonDoubleClick : MouseAction
    {
        public override MouseExCode ExCode
        {
            get { return MouseExCode.RightButtonDoubleClick; }
        }

        public override void Process(byte[] arg)
        {
            mouse.RightButtonDoubleClick();
        }
    }

}
